package com.huluobo.a2107kotlinshopping.cart

import androidx.lifecycle.MutableLiveData
import com.huluobo.a2107kotlinshopping.base.BaseRepo
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody

/**
 *  Created by LC on 2023/11/17.
 */
class CartRepo : BaseRepo() {
    fun getCartList(success: MutableLiveData<CartBeanResult>, failed: MutableLiveData<String>) {
        getService().getCartList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CartBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CartBeanResult) {
                    success.value = t
                }
            })
    }

    fun deleteCart(body: RequestBody, success: MutableLiveData<ChangeCartBeanResult>, failed: MutableLiveData<String>) {
        getService().deleteCart(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<ChangeCartBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: ChangeCartBeanResult) {
                    success.value = t
                }
            })
    }

    fun changeCartNum(
        body: RequestBody,
        success: MutableLiveData<ChangeCartBeanResult>,
        failed: MutableLiveData<String>
    ) {
        getService().changeCart(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<ChangeCartBeanResult> {
                override fun onSubscribe(d: Disposable) {

                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: ChangeCartBeanResult) {
                    success.value = t
                }
            })
    }
}